<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Firma il registro di classe</title>
<meta name="description" content="Il sito della scuola secondaria di primo grado di Iglesias, in Sardegna" />
<meta name="keywords" content="scuola, iglesias, didattica, insegnamento, docenti, scuola media" />
<link rel="stylesheet" href="andreas08.css" type="text/css" media="screen,projection" />
<script type="text/javascript" src="../../../js/prototype.js"></script>
<script type="text/javascript" src="../../../js/page.js"></script>
<script type="text/javascript">
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE);

var tempX = 0;
var tempY = 0;

var id_ore = new Array();
<?php 
while(list($k, $v) = each($ids)){
?>
id_ore[<?php print $k ?>] = <?php print $v ?>; 
<?php } ?>

function firma(event){
    //alert("ok");
    document.getElementById('hid').style.display = "none";
    var ora = document.forms[0].ora.value;
    var mat = document.forms[0].mat.value;
    var id_reg = document.forms[0].id_reg.value;
    var plus = document.forms[0].plus.value;
    var upd = 1;
    if(document.getElementById(ora+"ora").innerHTML == "Firma")
        upd = 0;
    var url = "firma.php";
    var req = new Ajax.Request(url,
			  {
			    	method:'post',
			    	parameters: {ora: ora, mat: mat, id_reg: id_reg, plus: plus, upd: upd},
			    	onSuccess: function(transport){
			      		var response = transport.responseText || "no response text";
			      		if(response == "ko"){
			      			_alert("Impossibile completare l'operazione richiesta. Riprovare tra qualche secondo o segnalare l'errore al webmaster");
				     		return;
			     		}
			     		else{
			     			var dati = response.split(";");
			                var campo = dati[1]+"ora";
			                var mat = dati[3];
			                var plus = dati[4];
			                var id_ora = dati[5];
			                var x = document.getElementById(campo);
			                if(plus == 0){
			                	x.innerHTML = "";
			                	x.innerHTML = mat;
			                }
			                else{
			                    mat = " / "+mat;
			                    x.innerHTML += mat;
			                }
			                /*
			                *	ora creo l'elemento a contenente il link di cancellazione
			                */
			               del_link = document.createElement("a");
			               del_link.setAttribute("style", "color: red");
			               del_link.setAttribute("href", "#");
			               del_link.setAttribute("id", dati[1]+"del");
			               del_link.setAttribute("onclick", "document.forms[0].ora.value = "+dati[1]+"; document.forms[0].plus.value = 0; del("+dati[1]+")");
			               del_link.appendChild(document.createTextNode(" (x) "));
			               //alert(x.parentNode);
			               x.parentNode.insertBefore(del_link, x.nextSibling);
			               id_ore[dati[1]] = dati[5];
			               visualizza_textarea();
			     		}
			    	},
			    	onFailure: function(){ _alert("Si e' verificato un errore...") }
			  });
}

function visualizza(e) {
    var hid = document.getElementById("hid");
    //alert(hid.style.top);
    if (IE) { // grab the x-y pos.s if browser is IE
        tempX = event.clientX + document.body.scrollLeft;
        tempY = event.clientY + document.body.scrollTop;
    } else {  // grab the x-y pos.s if browser is NS
        tempX = e.pageX;
        tempY = e.pageY;
    }  
    // catch possible negative values in NS4
    if (tempX < 0){tempX = 0;}
    if (tempY < 0){tempY = 0;}  
    hid.style.top = parseInt(tempY)+"px";
    //alert(hid.style.top);
    hid.style.left = parseInt(tempX)+"px";
    hid.style.display = "inline";
    return true;
}

function visualizza_r(e) {
    var hid = document.getElementById("hid");
    //alert(hid.style.top);
    if (IE) { // grab the x-y pos.s if browser is IE
        tempX = event.clientX + document.body.scrollLeft;
        tempY = event.clientY + document.body.scrollTop;
    } else {  // grab the x-y pos.s if browser is NS
        tempX = e.pageX;
        tempY = e.pageY;
    }  
    // catch possible negative values in NS4
    if (tempX < 0){tempX = 0;}
    if (tempY < 0){tempY = 0;}  
    tempX -= 240;
    hid.style.top = parseInt(tempY)+"px";
    //alert(hid.style.top);
    hid.style.left = parseInt(tempX)+"px";
    hid.style.display = "inline";
    return true;
}

function visualizza_textarea() {
	//alert("IN");
	var teacher_id = '<?php print $_SESSION['__user__']->getUid() ?>';
	var argomenti = new Array();
	<?php 
	while(list($k, $v) = each($argomenti)){
		if(is_array($v)){
			$v = '';	
		}
	?>
	argomenti[<?php print $k ?>] = '<?php print addslashes(utf8_decode($v)) ?>'; 
	<?php } ?>
	var teachers = new Array();
	<?php 
	while(list($k, $v) = each($firme)){
		list($a, $b) = split(";", $v);	
	?>
	teachers[<?php print $k ?>] = '<?php print $a ?>'; 
	<?php } ?>
	var ora = document.forms[0].ora.value;
	if(teachers[ora] != "Array" && teachers[ora] != teacher_id){
		return false;
	}
    var arg = prompt("Argomento della lezione", argomenti[ora]);
    if (arg != null && arg != ""){
    	
        var id_reg = document.forms[0].id_reg.value;
        var url = "arg.php";
        var req = new Ajax.Request(url,
  			  {
  			    	method:'post',
  			    	parameters: {ora: ora, id_reg: id_reg, arg: arg},
  			    	onSuccess: function(transport){
  			      		var response = transport.responseText || "no response text";
  			      		if(response == "ko"){
  			      			_alert("Impossibile completare l'operazione richiesta. Riprovare tra qualche secondo o segnalare l'errore al webmaster");
  				     		return;
  			     		}
  			     		else{
  			     			var dati = response.split(";");
  			                var campo = dati[1]+"ora_arg";
  			                //alert(campo);
  			                var x = document.getElementById(campo);
  			                x.innerHTML = "argomento inserito";
  			                x.setAttribute("onclick", "visualizza_textarea("+ora+")");
  			     		}
  			    	},
  			    	onFailure: function(){ alert("Si e' verificato un errore...");}
  			  });
    }
}

function del(id_registro){
	// cancella una firma
	var idr = id_ore[id_registro];
	alert(idr);
	var url = "firma.php";
	var ora = document.forms[0].ora.value;
    var id_reg = document.forms[0].id_reg.value;
    //alert(ora);
    //alert(id_reg);
    var req = new Ajax.Request(url,
			  {
			    	method:'post',
			    	parameters: {ora: ora, id_reg: id_reg, del: '1', del_id: idr},
			    	onSuccess: function(transport){
			      		var response = transport.responseText || "no response text";
			      		var dati = response.split("|");
			      		if(dati[0] == "ko"){
			      			//alert(dati[1]);
			      			//alert(dati[2]);
				     		return;
			     		}
			     		else{
			     			//ar dati = response.split(";");
			                var campo = dati[1]+"ora";
			                //alert(campo);
			                var x = document.getElementById(campo);
			                x.innerHTML = "Firma";
			                $(dati[1]+"del").innerHTML = "";
			                $(dati[1]+"ora_arg").innerHTML = "";
			     		}
			    	},
			    	onFailure: function(){ _alert("Si e' verificato un errore..."); }
			  });
}

</script>
</head>
<body style="background-color: white">
<!--
DIV nascosto che contiene le materie
-->
<div id="hid" style="position: absolute; width: 240px; height: 85px; display: none; border: 1px solid; background-color: rgb(245,245,245); padding-left: 10px">
<?php
$k = 0;
foreach($materie as $m){
?>
    <a style="font-weight: normal; <?php if($k%2) print('background-color: #E7E7E7') ?>" href="#" onclick="document.forms[0].mat.value = <?php print $m['id_materia'] ?>; firma(event); "><?php print $m['materia'] ?></a><br />
<?php
    $k++;
}
?>
</div>
<form>
<div style="text-align: center; font-weight: bold; padding-bottom: 25px"><?php print $_SESSION['__classe__']->to_string() ?><br />Registro di classe di <?php print ($giorno_str ." ". format_date($dati['data'], SQL_DATE_STYLE, IT_DATE_STYLE, "/")) ?></div>
<div style="width: 90%; text-align: left; margin: auto;">
<?php 
$i = 1;
foreach ($firme as $signature){
	$signatures = array();
	$f = split("#", $signature);
	foreach($f as $a){
		list($a1, $a2) = split(";", $a);
		array_push($signatures, $a2);
	}
	$mat_in = join(",", $signatures);
	$subjects = array();
	$sel_mat = "SELECT materia FROM materie WHERE id_materia IN ($mat_in)";
	try{
		$res_mat = $db->executeQuery($sel_mat);
		while($s = $res_mat->fetch_assoc()){
			array_push($subjects, $s['materia']);
		}
	} catch (MySQLException $ex){
		
	}
	
		
?>
<p style="width: 100%; padding-bottom: 5px; padding-top: 5px; <?php if($i%2) print("background-color: #e8eaec;") ?>">
<span style="padding-left: 10px; float: left"><?php print $i ?> ora</span>&nbsp;&nbsp;&nbsp;&nbsp;
<a id="<?php print $i."ora" ?>" style="color: black; font-weight: bold" href="#" onclick="document.forms[0].ora.value = '<?php print $i ?>'; document.forms[0].plus.value = 0; visualizza(event)"><?php if(count($subjects) > 0) print join(" / ", $subjects); else print "Firma" ?></a><?php if(count($subjects) > 0){ ?> <a id="<?php print $i."del" ?>" style="color: red" href="#" onclick="document.forms[0].ora.value = '<?php print $i ?>'; document.forms[0].plus.value = 0; del(<?php print $i ?>)">(x)</a><?php } ?>
<a id="<?php print $i."ora" ?>_plus" style="float: right; color: black; font-weight: bold; padding-right: 10px" href="#" onclick="document.forms[0].ora.value = '<?php print $i ?>'; document.forms[0].plus.value = 1; <?php if(count($subjects) > 0) {?>visualizza_r(event)<?php } else {?>alert('Ora non firmata')<?php } ?>"> + </a>
<a id="<?php print $i."ora" ?>_arg" style="float: right; padding-right: 100px; color: black; font-weight: normal; font-style: italic " href="#" onclick="document.forms[0].ora.value = '<?php print $i ?>'; document.forms[0].plus.value = 0; <?php if(count($subjects) > 0) {?>visualizza_textarea(<?php print $i ?>)<?php } else {?>alert('Ora non firmata')<?php } ?>">
<?php 
	if(count($subjects) > 0) {
		if($argomenti[$i] != ""){
			print("argomento inserito");
		}
		else {
			print("inserisci argomento");
		}
	} 
	else {
		print " ";
	} 
?>
</a>
</p>
<?php 
	$i++;
}
?>
</div>
<div style="float: right; margin-top: 30px; margin-right: 45px; ">
	<!-- <a href="#" onclick="window.close()">Chiudi</a>  -->
	<input type="hidden" name="ora" value="" />
	<input type="hidden" name="mat" value="" />
	<input type="hidden" name="plus" value="0" />
	<input type="hidden" name="id_reg" value="<?php print $_REQUEST['id_reg'] ?>" />
</div>
</form>
</body>
</html>